<?php

namespace zhtfrontend\models\datas;

use common\models\datas\Cate;
use Yii;
use common\components\helpers\Fun;
use yii\helpers\ArrayHelper;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
use common\components\extenders\MenuManage;
use common\components\helpers\Tree;

/**
 * This is the model class for table "{{%course}}".
 *
 * @property integer $ID
 * @property string $name
 * @property integer $mechince_id
 * @property integer $site_id
 * @property string $logo_img
 * @property string $longtime
 * @property string $info
 * @property string $notice
 * @property integer $user_id
 * @property integer $created_at
 * @property integer $is_book
 * @property string $teache_form
 * @property integer $is_artteaching
 */
class Course extends \zhtfrontend\components\bases\BaseModel
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%course}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['name', 'teache_form', 'mechince_id','category_id','type_id'], 'required'],
            [['category_id'], 'category_check'],
            [['mechince_id', 'site_id', 'user_id', 'created_at', 'updated_at', 'category_id','is_artteaching','status'], 'integer'],
            [['description'], 'string'],
            ['name', 'string', 'max' => 30],
            ['teache_form', 'string', 'max' => 100],
            [['mechince_id'],'check_Mechanic','on'=>['create','update']]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('zhtfrontend', 'ID'),
            'category_id' => Yii::t('zhtfrontend', '分类id'),
            'name' => Yii::t('zhtfrontend', '课程名'),
            'mechince_id' => Yii::t('zhtfrontend', '机构名称'),
            'site_id' => Yii::t('zhtfrontend', '分店编号'),
            'description' => Yii::t('zhtfrontend', '详情'),
            'user_id' => Yii::t('zhtfrontend', '操作员'),
            'created_at' => Yii::t('zhtfrontend', '创建时间'),
            'updated_at' => Yii::t('zhtfrontend', '修改时间'),
            'teache_form' => Yii::t('zhtfrontend', '教学形式'),
            'type_id' => Yii::t('zhtfrontend', '大类'),
            'is_artteaching' => Yii::t('zhtfrontend', '类型'),
            'status' => Yii::t('zhtfrontend', '状态'),
        ];
    }

    public function fields()
    {
        $fields = parent::fields();
        $fields['teache_form'] = function () {
            switch ($this->teache_form) {
                case 0:
                    return \Yii::t('zhtfrontend', '一对一');
                case 1:
                    return \Yii::t('zhtfrontend', '小班制');
                case 2:
                    return \Yii::t('zhtfrontend', '普通班');
                case 3:
                    return \Yii::t('zhtfrontend', '一对二');
                case 4:
                    return \Yii::t('zhtfrontend', '一对三');
            }
        };


        $fields['created_at'] = function () {
            return \Yii::$app->formatter->asDatetime($this->created_at);
        };
        $fields['updated_at'] = function () {
            return \Yii::$app->formatter->asDatetime($this->updated_at);
        };
        return $fields;
    }
    public function extraFields()
    {
        return [
            'category_name' => function () {
                return isset($this->cate->name)?$this->cate->name:'';
            },
            'admin' => function () {
                return $this->admin->username;
            },
            'mechanic_name' => function () {
                return ($this->mechanic != null) ? $this->mechanic->name : '';
            },
            'mechanic_id' => function () {
                return ($this->mechanic != null) ? $this->mechanic->id : '';
            },
            'operate' => function () {
                return (new MenuManage(MenuManage::RECORD_ITEM_TOOLBAR, $this->attributes))->isDisable(
                ['delete' => function ($data) {
                        $course_id = Arrangement::find()->select('course_id')->where("course_id!=''")->andWhere(['is_delete'=>0])->distinct('course_id')->column();
                        if ($course_id != null && in_array($data['id'], $course_id)) {
                            return '<button class="yx-btn-disabled" disabled>删除</button>';
                        } else {
                            return '';
                        }
                    },
                    'update' => function ($data) {
                        return '';
                    }
                ]
                )->create();
            },
        ];
    }

    public function behaviors()
    {
        return ArrayHelper::merge(parent::behaviors(), [
            [
                'class' => TimestampBehavior::className(),
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'],
                    ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'],
                ],
            ],
        ]);
    }

    public function check_Mechanic($attribute){
        $mechanic = Mechanic::find()->where(['parent_id'=>$this->mechince_id])->all();
        if($mechanic != null){
            $this->addError($attribute, Yii::t('zhtfrontend', '请选择子级机构'));
        }

    }

    public function getCate()
    {
        return $this->hasOne(Cate::className(), ['id' => 'category_id']);
    }

    public function getAdmin()
    {
        return $this->hasOne(Admin::className(), ['id' => 'user_id']);
    }

    public function getMechanic()
    {
        return $this->hasOne(Mechanic::className(), ['id' => 'mechince_id']);
    }

//    检测是否为二级分类
    public function category_check($attribute)
    {
        $categorys = Fun::cache('cate_cache', false);
        $tree = new Tree($categorys);
        $categoryLevel = $tree->get_level($this->$attribute);
        if ($categoryLevel == 1) {
            $this->addError($attribute, \Yii::t('zhtfrontend', '请选择二级分类'));
        }
    }
}
